package com.phb.puhuibao.web.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.idap.clinic.entity.UploadFile;
import com.idap.web.common.controller.Commons;
import com.idp.pub.entity.Pager;
import com.idp.pub.service.IBaseService;
import com.idp.pub.utils.RSAUtils;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.common.Functions;
import com.phb.puhuibao.common.InitListener;
import com.phb.puhuibao.entity.ExperienceValue;
import com.phb.puhuibao.entity.Invite;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.MobileUserExtra;
import com.phb.puhuibao.entity.MobileUserHexagram;
import com.phb.puhuibao.entity.MuserCollection;
import com.phb.puhuibao.entity.MuserFollow;
import com.phb.puhuibao.entity.UserCard;
import com.phb.puhuibao.entity.UserSession;
import com.phb.puhuibao.service.MobileUserService;
import com.phb.puhuibao.service.MuserCollectionService;
import com.phb.puhuibao.service.MuserJobReleaseService;
import com.phb.puhuibao.service.impl.MuserFollowServiceImpl;

@Controller
@RequestMapping(value = "/muserCollection")
public class MuserCollectionController extends BaseController<MuserCollection, String> {
	final Log log = LogFactory.getLog(MuserCollectionController.class);

	@Resource(name = "muserCollectionService")
	public void setBaseService(IBaseService<MuserCollection, String> baseService) {
		super.setBaseService(baseService);
	}

	@Resource(name = "mobileUserService")
	private IBaseService<MobileUser, String> mobileUserService;

	@Resource(name = "muserCollectionService")
	private MuserCollectionService muserCollectionService;

	// -----我的职位收藏-----
	@RequestMapping(value = "myCollection")
	@ResponseBody
	public Map<String, Object> myCollection(@RequestParam String userId, @RequestParam String collectionType,
			@RequestParam String pageno) {

		Map<String, Object> data = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			Pager<MuserCollection> pager = new Pager<MuserCollection>();
			pager.setCurrent(Integer.parseInt(pageno));
			params.put("userId", userId);
			params.put("collectionType", collectionType);
			params.put("orderBy", "create_time");
			Pager<MuserCollection> p = this.getBaseService().findByPager(pager, params);
			data.put("result", p.getData());
			data.put("message", "查询收藏成功！");
			data.put("status", 1);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询收藏失败:" + e);
			data.put("message", "注册保存失败！");
			data.put("status", 0);
		}
		return data;
	}

	// -----收藏 职位or企业-----
	@RequestMapping(value = "collectionJob")
	@ResponseBody
	public Map<String, Object> collectionJob(@RequestParam String userId, @RequestParam String jobId,
			                                 @RequestParam String companyId) {

		Map<String, Object> data = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", userId);
			MuserCollection col = new MuserCollection();
			MobileUser user = mobileUserService.unique(params);
			if (user == null) {
				data.put("message", "注册并成功登陆后再来收藏吧！");
				data.put("status", 0);
				return data;
			}else{
				col.setUserId(user.getUserId());
			}
			
			// 0:收藏职位 1:收藏企业
			if (jobId != null && !"".equals(jobId)) {
				col.setJobId(Integer.parseInt(jobId));
				col.setCollectionType(0);
			}
			if (companyId != null && !"".equals(companyId)) {
				col.setCompanyId(companyId);
				col.setCollectionType(1);
			}
			this.getBaseService().save(col);
			data.put("result", col);
			data.put("message", "收藏成功！");
			data.put("status", 1);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询收藏失败:" + e);
			data.put("message", "收藏失败！");
			data.put("status", 0);
		}
		return data;
	}

	// 我的收藏(0职位收藏1企业收藏)
	@RequestMapping(value = "getMuserJobs")
	@ResponseBody
	public Map<String, Object> getMuserJobs(@RequestParam String mUserId, @RequestParam String longt,
											@RequestParam String lat, @RequestParam String type, 
											@RequestParam String pageno) {
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> muserCollection = muserCollectionService.getMuserCollection(mUserId, longt, lat,type, pageno);
			if (!muserCollection.isEmpty()) {
				data.put("result", muserCollection);
				data.put("message", "查询成功");
				data.put("status", 1);
			} else {
				data.put("message", "数据为空");
				data.put("status", 2);
			}
		} catch (DataAccessException e) {
			data.put("message", "网络异常");
			data.put("status", 0);
		}
		return data;
	}
}
